{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "# 第 2 节　使用 Python 进行描述统计：多变量\n",
    "## 第 3 章　使用 Python 进行数据分析｜用 Python 动手学统计学"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 4. 多变量数据的管理"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [
    {
     "data": {
      "text/plain": "'%.3f'"
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用于数值计算的库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# 设置浮点数打印精度\n",
    "%precision 3"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.113110Z",
     "end_time": "2024-04-16T09:45:44.694445Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 5. 实现：求各分组的统计量"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "  species  length\n0       A       2\n1       A       3\n2       A       4\n3       B       6\n4       B       8\n5       B      10",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>species</th>\n      <th>length</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>A</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>A</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>A</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>B</td>\n      <td>6</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>B</td>\n      <td>8</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>B</td>\n      <td>10</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fish_multi = pd.read_csv('3-2-1-fish_multi.csv')\n",
    "fish_multi"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.696444Z",
     "end_time": "2024-04-16T09:45:44.707473Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "         length\nspecies        \nA           3.0\nB           8.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>length</th>\n    </tr>\n    <tr>\n      <th>species</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>3.0</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>8.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 按鱼的种类计算\n",
    "group = fish_multi.groupby(\"species\")\n",
    "group.mean()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.709469Z",
     "end_time": "2024-04-16T09:45:44.743940Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "        length                                    \n         count mean  std  min  25%  50%  75%   max\nspecies                                           \nA          3.0  3.0  1.0  2.0  2.5  3.0  3.5   4.0\nB          3.0  8.0  2.0  6.0  7.0  8.0  9.0  10.0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead tr th {\n        text-align: left;\n    }\n\n    .dataframe thead tr:last-of-type th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr>\n      <th></th>\n      <th colspan=\"8\" halign=\"left\">length</th>\n    </tr>\n    <tr>\n      <th></th>\n      <th>count</th>\n      <th>mean</th>\n      <th>std</th>\n      <th>min</th>\n      <th>25%</th>\n      <th>50%</th>\n      <th>75%</th>\n      <th>max</th>\n    </tr>\n    <tr>\n      <th>species</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>A</th>\n      <td>3.0</td>\n      <td>3.0</td>\n      <td>1.0</td>\n      <td>2.0</td>\n      <td>2.5</td>\n      <td>3.0</td>\n      <td>3.5</td>\n      <td>4.0</td>\n    </tr>\n    <tr>\n      <th>B</th>\n      <td>3.0</td>\n      <td>8.0</td>\n      <td>2.0</td>\n      <td>6.0</td>\n      <td>7.0</td>\n      <td>8.0</td>\n      <td>9.0</td>\n      <td>10.0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "group.describe()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.717159Z",
     "end_time": "2024-04-16T09:45:44.854311Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 6. 实现：列联表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "   store color  sales\n0  tokyo  blue     10\n1  tokyo   red     15\n2  osaka  blue     13\n3  osaka   red      9",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>store</th>\n      <th>color</th>\n      <th>sales</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>tokyo</td>\n      <td>blue</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>tokyo</td>\n      <td>red</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>osaka</td>\n      <td>blue</td>\n      <td>13</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>osaka</td>\n      <td>red</td>\n      <td>9</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "shoes = pd.read_csv('3-2-2-shoes.csv')\n",
    "shoes"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.737994Z",
     "end_time": "2024-04-16T09:45:44.857310Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "color  blue  red\nstore           \nosaka    13    9\ntokyo    10   15",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th>color</th>\n      <th>blue</th>\n      <th>red</th>\n    </tr>\n    <tr>\n      <th>store</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>osaka</th>\n      <td>13</td>\n      <td>9</td>\n    </tr>\n    <tr>\n      <th>tokyo</th>\n      <td>10</td>\n      <td>15</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cross = pd.pivot_table(data=shoes, values='sales', aggfunc='sum', index='store', columns='color')\n",
    "cross"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.744942Z",
     "end_time": "2024-04-16T09:45:44.866679Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 9. 实现：协方差"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "      x   y\n0  18.5  34\n1  18.7  39\n2  19.1  41\n3  19.7  38\n4  21.5  45\n5  21.7  41\n6  21.8  52\n7  22.0  44\n8  23.4  44\n9  23.8  49",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>x</th>\n      <th>y</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>18.5</td>\n      <td>34</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>18.7</td>\n      <td>39</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>19.1</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>19.7</td>\n      <td>38</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>21.5</td>\n      <td>45</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>21.7</td>\n      <td>41</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>21.8</td>\n      <td>52</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>22.0</td>\n      <td>44</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>23.4</td>\n      <td>44</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>23.8</td>\n      <td>49</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cov_data = pd.read_csv('3-2-3-cov.csv')\n",
    "cov_data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.755838Z",
     "end_time": "2024-04-16T09:45:44.889684Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [
    "# 读取数据的列\n",
    "x = cov_data[\"x\"]\n",
    "y = cov_data[\"y\"]\n",
    "\n",
    "# 求样本容量\n",
    "N = len(cov_data)\n",
    "\n",
    "# 求各变量均值\n",
    "mu_x = np.mean(x)\n",
    "mu_y = np.mean(y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.803168Z",
     "end_time": "2024-04-16T09:45:44.889684Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "data": {
      "text/plain": "6.906"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本协方差\n",
    "cov_sample = sum((x - mu_x) * (y - mu_y)) / N\n",
    "cov_sample"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.805165Z",
     "end_time": "2024-04-16T09:45:44.904706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "data": {
      "text/plain": "7.673"
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 协方差\n",
    "cov = sum((x - mu_x) * (y - mu_y)) / (N - 1)\n",
    "cov"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.812439Z",
     "end_time": "2024-04-16T09:45:44.904706Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 10. 实现：协方差矩阵"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 3.282,  6.906],\n       [ 6.906, 25.21 ]])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 样本协方差\n",
    "np.cov(x, y, ddof=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.818084Z",
     "end_time": "2024-04-16T09:45:44.905706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 3.646,  7.673],\n       [ 7.673, 28.011]])"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 无偏协方差\n",
    "np.cov(x, y, ddof=1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.823953Z",
     "end_time": "2024-04-16T09:45:44.905706Z"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 13. 实现：皮尔逊积矩相关系数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "data": {
      "text/plain": "0.759"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算两个变量的方差\n",
    "sigma_2_x = np.var(x, ddof=1)\n",
    "sigma_2_y = np.var(y, ddof=1)\n",
    "\n",
    "# 计算相关系数\n",
    "rho = cov / np.sqrt(sigma_2_x * sigma_2_y)\n",
    "rho"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.830800Z",
     "end_time": "2024-04-16T09:45:44.906706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[1.   , 0.759],\n       [0.759, 1.   ]])"
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 相关矩阵\n",
    "np.corrcoef(x, y)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.841690Z",
     "end_time": "2024-04-16T09:45:44.906706Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-04-16T09:45:44.846802Z",
     "end_time": "2024-04-16T09:45:44.906706Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
